package com.oncecloud.devops.client.controller;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.oncecloud.devops.client.entity.User;
import com.oncecloud.devops.client.service.UserService;
import com.oncecloud.devops.client.util.Result;


/**
 * 用户信息控制
 * @author xzg
 *
 */
@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	@ResponseBody
	public String getUsers(@RequestParam(value = "userName", required = false)String userName){
		return userService.getUsers(userName);
	}
	@RequestMapping(value = "/detail", method = { RequestMethod.GET })
	@ResponseBody
	public String getUserDetail(@RequestParam(value = "userId", required = false)Integer userId){
		return userService.getUserDetail(userId);
	}
	
	/**
	 * 创建用户
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	@ResponseBody
	public Result insertUser(HttpServletRequest request,User user,
			@RequestParam("envId") Integer defaultEnvId){
		//创建用户时添加一个默认的初始环境
		user.setDefaultEnvId(defaultEnvId);
		int re = userService.insert(user);
		if( 0 != re)
			return new Result(true, "创建用户成功");
		else
			return new Result(false, "创建用户失败");
	}
	/**
	 * 修改用户信息
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/updateUser", method = { RequestMethod.POST })
	@ResponseBody
	public Result updateUser(HttpServletRequest request,User user){
		
		int re = userService.updateUserByUserId(user);
		if( 0 != re)
			return new Result(true, "修改用户成功");
		else
			return new Result(false, "修改用户失败");
	}	
	/**
	 * 删除用户
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/deleteUser", method = { RequestMethod.GET })
	@ResponseBody
	public Result deleteUser(HttpServletRequest request,@RequestParam("userId")Integer userId){
		Result result = new Result();
		try {
			result.setMessage("删除成功");
			result.setSuccess(true);
			userService.delete(userId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			result.setMessage("删除失败");
			result.setSuccess(false);
			
		}
		return result;
	}		
	
	/**
	 * 获取普通用户信息
	 * @param request
	 * @param envId
	 * @return
	 */
	@RequestMapping(value = "/getOrdinaryleUser", method = { RequestMethod.GET})
	@ResponseBody
	public Result getOrdinaryleUser(HttpServletRequest request,@RequestParam("envId") Integer envId){
		
		List<User> list = userService.getOrdinaryleUser(envId);
		if(null == list || 0 == list.size())
			return new Result(false, "无返回结果！");
		else
			return new Result(true, list, "操作成功");
	}

	/**
	 * 环境下查看项目管理员
	 * @param request
	 * @param envId
	 * @return
	 */
	@RequestMapping(value = "/getprojectUser", method = { RequestMethod.GET})
	@ResponseBody
	public Result getprojectUser(HttpServletRequest request,@RequestParam("envId") Integer envId){
		
		List<User> list = userService.getProjectUser(envId);
		if(null == list || 0 == list.size())
			return new Result(false, "无返回结果！");
		else
			return new Result(true, list, "操作成功");
	}
	
	/**
	 * 获取普通用户信息
	 * @param request
	 * @param envId
	 * @return
	 */
	@RequestMapping(value = "/getEnvUser", method = { RequestMethod.GET})
	@ResponseBody
	public Result getEnvUser(HttpServletRequest request){
		
		List<User> list = userService.getEnvUser();
		if(null == list || 0 == list.size())
			return new Result(false, "无返回结果！");
		else
			return new Result(true, list, "操作成功");
	}
	
	
	
}
